#/usr/bin/bash
read -p "Please input password for redis: " pass
read -p "Please input port for redis: " port
if [[ ! "$port" =~ ^[1-9][0-9]{3,4}$ ]];then
	echo "port must gt 1000,such as:6379"
	exit
fi
rpm -qa |grep gcc* &>/dev/null
if [ $? -ne 0 ];then
	yum -y install gcc-c++ &>/dev/null
fi

gcc -v &>/dev/null

if [ $? -ne 0 ];then
	yum -y install gcc
fi
# 查看gcc版本,yum安装的4.8.5执行make会出错
gcc_version=$(gcc -v 2>&1 | grep '^gcc' | awk '{print $3}')
gversion=$(echo "$gcc_version" | cut -d'.' -f1)

if [[ $gversion -le 4 && $gcc_version =~ ^4\.8* ]];then
	yum -y install centos-release-scl
	yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
	scl enable devtoolset-9 bash
	#修改环境变量
	echo "source /opt/rh/devtoolset-9/enable" >> /etc/profile
fi

directory="/usr/lib/redis"

if [ ! -d $directory ];then
	mkdir -p $directory &>/dev/null
fi

cd $directory

redisfile="redis-6.0.8.tar.gz"
wget https://mirrors.huaweicloud.com/redis/${redisfile}

if [ ! -f ${directory}/${redisfile} ];then
	echo "download redis error"
	exit
fi
tar -zxvf ${directory}/${redisfile}

if [ ! -d ${directory}/${redisfile:0:11} ];then
	echo "tar error"
	exit
fi

cd  ${directory}/${redisfile:0:11}

installdirectory="/usr/local/redis"
make && make PREFIX=${installdirectory} install

ls ${installdirectory}/bin |grep "redis"
if [ $? -ne 0 ];then
	echo "install error"
	exit
fi
cp ${directory}/${redisfile:0:11}/redis.conf ${installdirectory}

redis_conf="${installdirectory}/redis.conf"
sed -i 's/^bind 127.0.0.1/#&/' ${redis_conf}
sed -i 's/^protected-mode yes/protected-mode no/' ${redis_conf}
sed -i 's/^daemonize no/daemonize yes/' ${redis_conf}
sed -i "s/^# *requirepass .*/requirepass ${pass}/" ${redis_conf}
sed -i "s/^port .*/port ${port}/" /usr/local/redis/redis.conf
${installdirectory}/bin/redis-server ${redis_conf}

clear
rm -rf ${directory}
echo "install directory: ${installdirectory}"
echo "redis.conf: ${redis_conf}"
